<?php

namespace app\data\controller;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use app\data\model\DataDevice;
use app\data\service\DeviceService;
/**
 * 设备信息管理
 * @class Hotel
 * @package app\data\controller
 */
class Device extends Controller
{
    /**
     * 设备信息管理
     * @menu true
     * @auth true
     */
    public function index()
    {
        DataDevice::mQuery()->layTable(function () {
            $this->title = '设备信息管理管理';
        }, function (QueryHelper $query) {
            // 数据列表搜索过滤
            $query->where(['is_deleted'=>0]);
            $query->equal('status')->dateBetween('create_at#create_at');
            $query->like('Nickname');
        });
    }
    /**
     * 添加设备
     * @auth true
     */
    public function add()
    {
        if ($this->request->isGet()) {
            return $this->fetch();
        }else{
            $data=[
                'productKey'=>config('iot.ProductKey'),
                'deviceName'=>input('param.deviceName'),
                'nickname'=>input('param.nickname')
            ];
            $mod = new DeviceService();
            $response = $mod->registerDevice($data);
            if($response['err_code'] == 1){
                $this->error($response['data']);
            }else{
                $res = $response['data'];
                if($res->body->code !=''){
                    $this->error($res->body->errorMessage);
                }else{
                    //这里与 服务端订阅添加消息重复,会添加2条, 服务端订阅测试稳定后可删除这段插入
                    $check =  DataDevice::mk()->where(['iotId'=>$res->body->data->iotId])->count();
                    if($check == 0){
                        $backData=[
                            'deviceName'=>$res->body->data->deviceName,
                            'deviceSecret'=>$res->body->data->deviceSecret,
                            'iotId'=>$res->body->data->iotId,
                            'nickname'=>$res->body->data->nickname,
                            'productKey'=>$res->body->data->productKey,
                        ];
                        DataDevice::mk()->insert($backData);
                    }
                    $this->success('添加成功');
                }
            }
        }
    }
    /**
     * 同步设备数据
     * @auth true
     */
    public function sync()
    {
        sysoplog('设备管理', '创建设备同步任务');
        $this->_queue('同步设备数据', "Aliyun:syncDevice");
    }
    /**
     * 删除设备
     * @auth true
     */
    public function remove()
    {
        $iotId= input('param.iotId');
        $mod = new DeviceService();
        $response = $mod->delDevice(['iotId'=>$iotId]);
        if($response['err_code'] == 1){
            $this->error($response['data']);
        }else{
            $res = $response['data'];
            if($res->body->code !=''){
                $this->error($res->body->errorMessage);
            }else{
                DataDevice::mk()->where(['iotId'=>$iotId])->update(['is_deleted'=>1]);
                $this->success('删除成功');
            }
        }
    }
}
